<?php namespace App\Services;

use Sentinel;
use Auth;
use App\Exceptions\ValidateParameterException;
use Cartalyst\Sentinel\Checkpoints\ThrottlingException;

class UserService{

	public function toLogin($data,$remember){
		try {
			$user = Sentinel::authenticate($data);

			if(!$user){
				throw new ValidateParameterException('账号或者密码不正确');
			}

			Sentinel::login($user,$remember);
		} catch (ThrottlingException $e) {

			$min = floor($e->getDelay()/60);

			throw new ValidateParameterException("登录失败次数超过限制，请{$min}分钟后再登录");
		}
	}

	public function toLoginHotel($data){
		if (!Auth::attempt(['username' => $data['username'], 'password' => $data['password']])) {
            throw new ValidateParameterException('账号或者密码不正确');
        }
	}

	public function logout(){

    	LoginService::logout();

    	return formatJsonResponse();
    }
}